package com.qiaofang.applet.service.property.service.mapstruct;

import com.qiaofang.applet.constants.FirstEstateSaleStautsConstant;
import com.qiaofang.applet.service.other.util.DateTypeUtil;
import com.qiaofang.applet.service.property.domain.EstateHouseType;
import com.qiaofang.applet.service.property.domain.EstateImage;
import com.qiaofang.applet.service.property.domain.FirsthandEstate;
import com.qiaofang.applet.service.property.service.dto.*;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface FirsthandEstateMapper {
    default FirsthandEstateListDto domainToListDto(FirsthandEstate firsthandEstate) {
        //处理显示图片
        FirsthandEstateListDto firsthandEstateListDto = new FirsthandEstateListDto();
        firsthandEstateListDto.setEstateUuid(firsthandEstate.getEstateUuid());
        firsthandEstateListDto.setImgUrl(firsthandEstate.getImageUrl());
        firsthandEstateListDto.setEstateName(firsthandEstate.getEstateName());
        firsthandEstateListDto.setAddress(firsthandEstate.getAddress());
        firsthandEstateListDto.setCityName(firsthandEstate.getCityName());
        firsthandEstateListDto.setAreaName(firsthandEstate.getAreaName());
        firsthandEstateListDto.setAveragePrice(firsthandEstate.getAveragePrice());
        firsthandEstateListDto.setSaleStatusName(getSaleStatusName(firsthandEstate));
        firsthandEstateListDto.setBuildingAreaRange(firsthandEstate.getBuildingAreaRange());
        firsthandEstateListDto.setBedroomList(getBedroomList(firsthandEstate));
        firsthandEstateListDto.setFeatureTypeList(splitString(firsthandEstate.getFeatureType()));
        firsthandEstateListDto.setPropertyTypeList(splitString(firsthandEstate.getPropertyType()));
        firsthandEstateListDto.setEstateUniqueUuid(firsthandEstate.getEstateUniqueUuid());
        return firsthandEstateListDto;
    }

    default FirsthandEstateDetailDto domainToDetailDto(FirsthandEstate firsthandEstate) {
        FirsthandEstateDetailDto firsthandEstateDetailDto = new FirsthandEstateDetailDto();
        BeanUtils.copyProperties(firsthandEstate, firsthandEstateDetailDto);
        firsthandEstateDetailDto.setSaleStatusName(getSaleStatusName(firsthandEstate));
        firsthandEstateDetailDto.setBuildingAreaRange(firsthandEstate.getBuildingAreaRange());
        return firsthandEstateDetailDto;
    }

    default FirsthandEstateAdminListDto domainToAdminListDto(FirsthandEstate firsthandEstate) {
        FirsthandEstateAdminListDto firsthandEstateAdminListDto = new FirsthandEstateAdminListDto();
        BeanUtils.copyProperties(firsthandEstate, firsthandEstateAdminListDto);
        firsthandEstateAdminListDto.setSaleStatusName(getSaleStatusName(firsthandEstate));
        firsthandEstateAdminListDto.setBuildingAreaRange(firsthandEstate.getBuildingAreaRange());
        return firsthandEstateAdminListDto;
    }

    default Page<FirsthandEstateListDto> convertDomainToListDto(Page<FirsthandEstate> firsthandEstatePage) {
        List<FirsthandEstateListDto> content = new ArrayList<>();

        if (firsthandEstatePage.getTotalElements() > 0) {
            content = firsthandEstatePage.getContent().parallelStream().map(this::domainToListDto).collect(Collectors.toList());
        }

        return PageableExecutionUtils.getPage(content, firsthandEstatePage.getPageable(), () -> firsthandEstatePage.getTotalElements());
    }

    //封面图
    default String getCoverImageUrl(FirsthandEstate firsthandEstate) {
        List<EstateImage> images = firsthandEstate.getImageList();
        if (CollectionUtils.isEmpty(images)) {
            return "";
        }
        List<String> urls = images.stream().filter(item -> item.getImageType().equals("1")).map(EstateImage::getImageUrl).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(urls)) {
            return "";
        }
        return urls.get(0);
    }

    //分割返回list
    default List<String> splitString(String str) {
        if (!StringUtils.isEmpty(str)) {
            return Arrays.asList(str.split("\\|"));
        } else {
            return new ArrayList<>();
        }
    }


    //户型室
    default List<String> getBedroomList(FirsthandEstate firsthandEstate) {
        List<EstateHouseType> estateHouseTypeList = firsthandEstate.getHouseTypeList();
        if (CollectionUtils.isEmpty(estateHouseTypeList)) {
            return new ArrayList<>();
        }
        List<String> bedroomList = new ArrayList<>(estateHouseTypeList.stream().map(EstateHouseType::getBedroom).collect(Collectors.toSet()));
        return bedroomList;
    }

    //设置最小和最大建筑面积
    default void setMinAndMaxBuildingArea(FirsthandEstate firsthandEstate) {
        List<EstateHouseType> estateHouseTypeList = firsthandEstate.getHouseTypeList();
        if (CollectionUtils.isEmpty(estateHouseTypeList)) {
            return;
        }
        List<BigDecimal> list = estateHouseTypeList.stream().map(EstateHouseType::getBuildingArea).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        BigDecimal min = list.get(0);
        BigDecimal max = list.get(0);
        for (BigDecimal temp : list) {
            if (temp.compareTo(max) > 0) {
                max = temp;
            }
            if (temp.compareTo(min) < 0) {
                min = temp;
            }
        }
        firsthandEstate.setMinBuildingArea(min);
        firsthandEstate.setMaxBuildingArea(max);
    }

    //户型面积范围
    default String getBuildingAreaRange(FirsthandEstate firsthandEstate) {
        if (!Objects.isNull(firsthandEstate.getMinBuildingArea()) && !Objects.isNull(firsthandEstate.getMinBuildingArea())) {
            return String.format("%s-%s", firsthandEstate.getMinBuildingArea(), firsthandEstate.getMaxBuildingArea());
        }
        return "";
    }

    //销售状态名称
    default String getSaleStatusName(FirsthandEstate firsthandEstate) {
        if (StringUtils.isEmpty(firsthandEstate.getSaleStatus())) {
            return "";
        }
        if (FirstEstateSaleStautsConstant.SALE_STATUS_1.equals(firsthandEstate.getSaleStatus())) {
            String result = "未开盘";
            try {
                if (firsthandEstate.getOpenDate() != null && DateTypeUtil.daysBetween(firsthandEstate.getOpenDate(), new Date()) < 30) {
                    result = "即将开盘";
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return result;
        }
        if (FirstEstateSaleStautsConstant.SALE_STATUS_6.equals(firsthandEstate.getSaleStatus())) {
            return "售罄";
        }
        return "在售";
    }

    default FirstEstateSubscribeDto domainToSubscribeListDto(FirsthandEstate firsthandEstate) {
        //处理显示图片
        FirstEstateSubscribeDto firstEstateSubscribeDto = new FirstEstateSubscribeDto();
        firstEstateSubscribeDto.setEstateUuid(firsthandEstate.getEstateUuid());
        firstEstateSubscribeDto.setImgUrl(firsthandEstate.getImageUrl());
        firstEstateSubscribeDto.setEstateName(firsthandEstate.getEstateName());
        firstEstateSubscribeDto.setAddress(firsthandEstate.getAddress());
        firstEstateSubscribeDto.setCityName(firsthandEstate.getCityName());
        firstEstateSubscribeDto.setAreaName(firsthandEstate.getAreaName());
        firstEstateSubscribeDto.setAveragePrice(firsthandEstate.getAveragePrice());
        firstEstateSubscribeDto.setSaleStatusName(getSaleStatusName(firsthandEstate));
        firstEstateSubscribeDto.setBuildingAreaRange(firsthandEstate.getBuildingAreaRange());
        firstEstateSubscribeDto.setBedroomList(getBedroomList(firsthandEstate));
        firstEstateSubscribeDto.setFeatureTypeList(splitString(firsthandEstate.getFeatureType()));
        firstEstateSubscribeDto.setPropertyTypeList(splitString(firsthandEstate.getPropertyType()));
        firstEstateSubscribeDto.setEstateUniqueUuid(firsthandEstate.getEstateUniqueUuid());
        return firstEstateSubscribeDto;
    }

    default List<FirsthandEstateComparedDetailDto> domainListToList(List<FirsthandEstate> firsthandEstateList){
        if ( firsthandEstateList == null ) {
            return null;
        }

        List<FirsthandEstateComparedDetailDto> list = new ArrayList<FirsthandEstateComparedDetailDto>( firsthandEstateList.size() );
        for ( FirsthandEstate firsthandEstate : firsthandEstateList ) {
            list.add( firsthandEstateToFirsthandEstateComparedDetailDto( firsthandEstate ) );
        }

        return list;
    }

    default FirsthandEstateComparedDetailDto firsthandEstateToFirsthandEstateComparedDetailDto(FirsthandEstate firsthandEstate) {
        if ( firsthandEstate == null ) {
            return null;
        }

        FirsthandEstateComparedDetailDto firsthandEstateComparedDetailDto = new FirsthandEstateComparedDetailDto();

        firsthandEstateComparedDetailDto.setEstateUniqueUuid( firsthandEstate.getEstateUniqueUuid() );
        firsthandEstateComparedDetailDto.setEstateName( firsthandEstate.getEstateName() );
        firsthandEstateComparedDetailDto.setBuildingType( firsthandEstate.getBuildingType() );
        firsthandEstateComparedDetailDto.setCityName( firsthandEstate.getCityName() );
        firsthandEstateComparedDetailDto.setAreaName( firsthandEstate.getAreaName() );
        firsthandEstateComparedDetailDto.setAveragePrice( firsthandEstate.getAveragePrice() );
        List<EstateHouseType> list = firsthandEstate.getHouseTypeList();
        if ( list != null ) {
            firsthandEstateComparedDetailDto.setHouseTypeList( new ArrayList<EstateHouseType>( list ) );
        }
        firsthandEstateComparedDetailDto.setAddress( firsthandEstate.getAddress() );
        firsthandEstateComparedDetailDto.setPropertyType( firsthandEstate.getPropertyType() );
        firsthandEstateComparedDetailDto.setPropertyRight( firsthandEstate.getPropertyRight() );
        firsthandEstateComparedDetailDto.setOpenDate( firsthandEstate.getOpenDate() );
        firsthandEstateComparedDetailDto.setCheckOutDate( firsthandEstate.getCheckOutDate() );
        firsthandEstateComparedDetailDto.setDevelopers( firsthandEstate.getDevelopers() );
        firsthandEstateComparedDetailDto.setPropertyFee( firsthandEstate.getPropertyFee() );
        firsthandEstateComparedDetailDto.setManageCompany( firsthandEstate.getManageCompany() );
        firsthandEstateComparedDetailDto.setUpCarPort( firsthandEstate.getUpCarPort() );
        firsthandEstateComparedDetailDto.setDownCarPort( firsthandEstate.getDownCarPort() );
        firsthandEstateComparedDetailDto.setGreeningRate( firsthandEstate.getGreeningRate() );
        firsthandEstateComparedDetailDto.setPlotRatio( firsthandEstate.getPlotRatio() );
        firsthandEstateComparedDetailDto.setHouseHolds( firsthandEstate.getHouseHolds() );
        firsthandEstateComparedDetailDto.setFeatureType( firsthandEstate.getFeatureType() );
        firsthandEstateComparedDetailDto.setEstateDesc( firsthandEstate.getEstateDesc() );
        firsthandEstateComparedDetailDto.setSaleStatusName(getSaleStatusName(firsthandEstate));
        firsthandEstateComparedDetailDto.setImgUrl(firsthandEstate.getImageUrl());

        return firsthandEstateComparedDetailDto;
    }
}
